package com.rssreader.client.mvc.views;

import com.extjs.gxt.ui.client.event.EventType;
import com.extjs.gxt.ui.client.mvc.AppEvent;
import com.extjs.gxt.ui.client.mvc.Dispatcher;
import com.extjs.gxt.ui.client.mvc.View;
import com.extjs.gxt.ui.client.widget.Status;
import com.extjs.gxt.ui.client.widget.toolbar.ToolBar;
import com.rssreader.client.mvc.controllers.StatusController;
import com.rssreader.client.mvc.events.AppEvents;
import com.rssreader.shared.model.Feed;
import com.rssreader.shared.model.Item;

public class StatusView extends View {

    private final Status status = new Status();
    private final ToolBar toolBar = new ToolBar();

    public StatusView(StatusController controller) {
	super(controller);
    }

    @Override
    protected void handleEvent(AppEvent event) {
	EventType eventType = event.getType();
	if (eventType.equals(AppEvents.Init)) {
	    onInit();
	    setStatus("Init");
	} else if (eventType.equals(AppEvents.FeedSelected)) {
	    Feed feed = event.getData();
	    setStatus("Feed Selected - (" + feed.getTitle() + ")");
	} else if (eventType.equals(AppEvents.ItemSelected)) {
	    Item item = event.getData();
	    setStatus("Item Selected - (" + item.getTitle() + ")");
	}
    }

    private void onInit() {
	// 裝配Status和ToolBar
	status.setWidth("100%");
	status.setBox(true);
	toolBar.add(status);
	// 派發StatusToolbarReady事件並加載toolBar
	Dispatcher.forwardEvent(new AppEvent(AppEvents.StatusToolbarReady, toolBar));
    }

    public void setStatus(String message) {
	status.setText(message);
    }

}
